﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PTA.Web
{
    public class EllipsisLabel : System.Web.UI.WebControls.Label
    {
        private readonly static EllipsisStyle ellipsis = new EllipsisStyle();
        protected override void OnPreRender(EventArgs e)
        {
            if (Page != null && Page.Header != null)
            {
                if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(EllipsisLabel), "SPAN_ELLIPSIS"))
                {
                    Page.Header.StyleSheet.CreateStyleRule(ellipsis, null, "span.ellipsis");
                    Page.ClientScript.RegisterClientScriptBlock(typeof(EllipsisLabel), "SPAN_ELLIPSIS", string.Empty, false);
                }
                this.CssClass = string.IsNullOrWhiteSpace(this.CssClass) ? "ellipsis" : string.Format("ellipsis {0}", this.CssClass.Trim());
            }
            else
            {
                this.MergeStyle(ellipsis);
            }
            base.OnPreRender(e);
        }
    }
    internal sealed class EllipsisStyle : System.Web.UI.WebControls.Style
    {
        protected override void FillStyleAttributes(System.Web.UI.CssStyleCollection attributes, System.Web.UI.IUrlResolutionService urlResolver)
        {
            base.FillStyleAttributes(attributes, urlResolver);
            attributes.Add(System.Web.UI.HtmlTextWriterStyle.Display, "inline-block");
            attributes.Add(System.Web.UI.HtmlTextWriterStyle.Overflow, "hidden");
            attributes.Add(System.Web.UI.HtmlTextWriterStyle.TextOverflow, "ellipsis");
            attributes.Add(System.Web.UI.HtmlTextWriterStyle.WhiteSpace, "nowrap");
            attributes.Add(System.Web.UI.HtmlTextWriterStyle.Width, "40px");
            attributes.Add(System.Web.UI.HtmlTextWriterStyle.Padding, "0px 3px");
        }
    }
}
